package com.zyj.report.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zyj.common.core.text.Convert;
import com.zyj.common.utils.DateUtils;
import com.zyj.report.domain.AssetsReportRepairs;
import com.zyj.report.mapper.AssetsReportRepairsMapper;
import com.zyj.report.service.IAssetsReportRepairsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 维修报统计Service业务层处理
 * 
 * @author zhuyujie
 * @date 2022-05-14
 */
@Service
public class AssetsReportRepairsServiceImpl implements IAssetsReportRepairsService
{
    @Autowired
    private AssetsReportRepairsMapper assetsReportRepairsMapper;

    /**
     * 查询维修报统计
     * 
     * @param id 维修报统计主键
     * @return 维修报统计
     */
    @Override
    public AssetsReportRepairs selectAssetsReportRepairsById(Long id)
    {
        return assetsReportRepairsMapper.selectAssetsReportRepairsById(id);
    }

    /**
     * 查询维修报统计列表
     * 
     * @param assetsReportRepairs 维修报统计
     * @return 维修报统计
     */
    @Override
    public List<AssetsReportRepairs> selectAssetsReportRepairsList(AssetsReportRepairs assetsReportRepairs)
    {
        return assetsReportRepairsMapper.selectAssetsReportRepairsList(assetsReportRepairs);
    }

    /**
     * 新增维修报统计
     * 
     * @param assetsReportRepairs 维修报统计
     * @return 结果
     */
    @Override
    public int insertAssetsReportRepairs(AssetsReportRepairs assetsReportRepairs)
    {
        assetsReportRepairs.setCreateTime(DateUtils.getNowDate());
        return assetsReportRepairsMapper.insertAssetsReportRepairs(assetsReportRepairs);
    }

    /**
     * 修改维修报统计
     * 
     * @param assetsReportRepairs 维修报统计
     * @return 结果
     */
    @Override
    public int updateAssetsReportRepairs(AssetsReportRepairs assetsReportRepairs)
    {
        return assetsReportRepairsMapper.updateAssetsReportRepairs(assetsReportRepairs);
    }

    /**
     * 批量删除维修报统计
     * 
     * @param ids 需要删除的维修报统计主键
     * @return 结果
     */
    @Override
    public int deleteAssetsReportRepairsByIds(String ids)
    {
        return assetsReportRepairsMapper.deleteAssetsReportRepairsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除维修报统计信息
     * 
     * @param id 维修报统计主键
     * @return 结果
     */
    @Override
    public int deleteAssetsReportRepairsById(Long id)
    {
        return assetsReportRepairsMapper.deleteAssetsReportRepairsById(id);
    }

    //统计维修数据
    public AssetsReportRepairs selectAssetsReportRepairs(){
        AssetsReportRepairs arr = new AssetsReportRepairs();
        long allnum = assetsReportRepairsMapper.selectAssetsReportRepairsAll();
        arr.setAllNum(allnum);
        long assetsAllNum = assetsReportRepairsMapper.selectAssetsReportAssetsAll();
        arr.setAssetsAllNum(assetsAllNum);
        arr.setRepairsNum(0L);
        arr.setScrepNum(0L);
        arr.setDoneNum(0L);
        List<AssetsReportRepairs> repairs = assetsReportRepairsMapper.selectAssetsReportRepairs();
        if(repairs!=null && !repairs.isEmpty()){
            for(int i=0;i<repairs.size();i++){
                AssetsReportRepairs ar = repairs.get(i);
                long id = ar.getId();
                long num = ar.getAllNum();
                if(id==1){
                    arr.setRepairsNum(num);
                }else if(id==2){
                    arr.setScrepNum(num);
                }else if(id==3){
                    arr.setDoneNum(num);
                }
            }
        }
        return arr;
    }

    public List<AssetsReportRepairs> selectAssetsReport(){
        List<AssetsReportRepairs> repairs = assetsReportRepairsMapper.selectAssetsReport();
        return repairs;
    }


    public List<AssetsReportRepairs> selectAssetsTypeReport(){
        List<AssetsReportRepairs> repairs = assetsReportRepairsMapper.selectAssetsTypeReport();
        return repairs;
    }

    public List<AssetsReportRepairs> assetsSatausReportlist(AssetsReportRepairs assetsReportRepairs){
        //获取入库的数量
        long initnum = 0;
        List<AssetsReportRepairs> list1 = assetsReportRepairsMapper.assetsInitReportlist(assetsReportRepairs);
        if(list1!=null && !list1.isEmpty()) {
            for (int i = 0; i < list1.size(); i++) {
                AssetsReportRepairs ar = list1.get(i);
                initnum = ar.getAllNum();
            }
        }
        AssetsReportRepairs arinit = new AssetsReportRepairs();
        arinit.setDonePercent("入库");
        arinit.setAllNum(initnum);

        List<AssetsReportRepairs> list = assetsReportRepairsMapper.assetsSatausReportlist(assetsReportRepairs);
        if(list!=null && !list.isEmpty()){
            for(int i=0;i<list.size();i++){
                AssetsReportRepairs ar = list.get(i);
                long id = ar.getId();
                if(id == 1){
                    ar.setDonePercent("领用");
                }else if(id == 2){
                    ar.setDonePercent("借用");
                }else if(id == 3){
                    ar.setDonePercent("归还");
                }else if(id == 4){
                    ar.setDonePercent("报废");
                }
            }
            list.add(arinit);
        }else{
            list = new ArrayList<AssetsReportRepairs>();
            list.add(arinit);
        }
        return list;
    }
}
